//
//  GroupUserCell.m
//  ChangChat
//
//  Created by Cocoa on 2019/9/24.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "GroupUserCell.h"
#import <UIButton+WebCache.h>

#define GroupUserCell_Btn_Tag 8745
#define GroupUserCell_Img_Tag 9745
#define GroupUserCell_Label_Tag 7745

@interface GroupUserCell ()
@property (nonatomic, strong) MASConstraint *addBtnRightLayout;
@property (nonatomic, strong) MASConstraint *deleteBtnWidthLayout;
@end
@implementation GroupUserCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setupUI {
    
    self.titleLabel = [UILabel k_labelWithText:@"群聊成员" fontSize:15 textColor:UIColor.k_t2Color];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
    }];
    
    CGFloat spaceX = (ScreenWidth - 15*2 - 31*2 - 15*2 - 50*4)/3;
    
    
    UIButton *lastBtn = nil;
    NSMutableArray *btnArr = [NSMutableArray arrayWithCapacity:4];
    for (int i = 0; i < 4; i++) {
        UIButton *btn = UIButton.k_initType(UIButtonTypeCustom).k_font([UIFont systemFontOfSize:14]).k_titleColor(UIColor.k_t2Color);
        
        btn.tag = GroupUserCell_Btn_Tag + i;
//        btn.k_title(@"某某某");
//        [btn sd_setImageWithURL:[@"" toURL] forState:UIControlStateNormal placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        @weakify(self);
        UIImageView *imgV = [UIImageView new];
        imgV.tag = GroupUserCell_Img_Tag + i;
        imgV.layer.cornerRadius = 22;
        imgV.layer.masksToBounds = YES;
        UILabel *tLb = [UILabel k_labelWithFontSize:14 textColor:UIColor.k_t2Color];
        tLb.textAlignment = NSTextAlignmentCenter;
        tLb.tag = GroupUserCell_Label_Tag + i;
        [btn addSubview:imgV];
        [btn addSubview:tLb];
        [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_offset(0);
            make.top.mas_offset(5);
            make.size.mas_equalTo(CGSizeMake(44, 44));
        }];
        [tLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_offset(0);
            make.top.equalTo(imgV.mas_bottom).offset(5);
        }];

        [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.didClickUserBlock) {
                self.didClickUserBlock(x.tag - GroupUserCell_Btn_Tag);
            }
        }];
        [self.contentView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(15);
            make.size.mas_equalTo(CGSizeMake(50, 70));
            if (lastBtn == nil) {
                make.left.mas_offset(15);
            } else {
                make.left.equalTo(lastBtn.mas_right).offset(spaceX);
            }
        }];
        
        lastBtn = btn;
        [btnArr addObject:btn];
    }
    
    
    self.addBtn = UIButton.k_initType(UIButtonTypeCustom).k_font([UIFont systemFontOfSize:15]).k_titleColor(UIColor.k_t2Color);
    self.addBtn.k_title(@"添加").k_img(UIImage.k_imgName(@"group_add_user"));
    
    self.deleteBtn = UIButton.k_initType(UIButtonTypeCustom).k_font([UIFont systemFontOfSize:15]).k_titleColor(UIColor.k_t2Color);
    self.deleteBtn.k_title(@"删除").k_img(UIImage.k_imgName(@"group_delete_user"));
    self.lookMoreBtn = UIButton.k_initType(UIButtonTypeSystem).k_font([UIFont systemFontOfSize:13]);
//    self.lookMoreBtn.k_titleColor(UIColor.k_t4Color);
    self.lookMoreBtn.tintColor = UIColor.k_t4Color;
    self.lookMoreBtn.k_title(@"查看全部成员").k_img(UIImage.k_imgName(@"arrow_right"));
    
    [self.contentView addSubview:self.addBtn];
    [self.contentView addSubview:self.deleteBtn];
    [self.contentView addSubview:self.lookMoreBtn];
    
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        self.addBtnRightLayout = make.right.equalTo(self.deleteBtn.mas_left).offset(-15);
        make.bottom.equalTo(lastBtn);
        make.size.mas_equalTo(CGSizeMake(31, 31));
    }];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.bottom.equalTo(lastBtn);
       self.deleteBtnWidthLayout = make.width.mas_equalTo(31);
        make.height.mas_equalTo(31);
    }];
    
    [self.lookMoreBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.mas_offset(-15);
    }];
    [lastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-15);
    }];
    
    
//    UIView *bottomView = UIView.new;
//    bottomView.backgroundColor = UIColor.k_hexColor(0xf4f4f4);
//    [self.contentView addSubview:bottomView];
//    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_offset(0);
//        make.top.equalTo(lastBtn.mas_bottom).offset(15);
//        make.height.mas_equalTo(10);
//        make.bottom.mas_offset(0);
//    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];

//    for (UIView *view in self.contentView.subviews) {
//        if (view.tag >= GroupUserCell_Btn_Tag) {
//            UIButton *btn = (UIButton *)view;
//            [btn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:5];
//        }
//    }
    [self.lookMoreBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:3];
}

- (void)setGroup:(GroupItem *)group {
    _group = group;
    GroupItem *item = (GroupItem *)group;
    if (group.manager == YES) {
        self.deleteBtn.hidden = NO;
        self.deleteBtnWidthLayout.offset(31);
        self.addBtnRightLayout.offset(-15);
    } else {
        self.deleteBtn.hidden = YES;
        self.deleteBtnWidthLayout.offset(0);
        self.addBtnRightLayout.offset(0);
    }
    
    int i = 0;
    for (FriendItem *obj in item.group_users) {
        UIButton *btn = [self.contentView viewWithTag:GroupUserCell_Btn_Tag+i];
        btn.hidden = NO;
        UILabel *lb = [btn viewWithTag:GroupUserCell_Label_Tag+i];
        lb.text = obj.displayName;
        UIImageView *imgV = [btn viewWithTag:GroupUserCell_Img_Tag+i];
        [imgV sd_setImageWithURL:obj.avatar.toURL placeholderImage:UIImage.k_imgName(@"placeholder_user")];
        i++;
    }
    
    while (i < 4) {
        UIButton *btn = [self.contentView viewWithTag:GroupUserCell_Btn_Tag+i];
        btn.hidden = YES;
        i++;
    }
    
    
}

@end
